import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { NzMessageService } from 'ng-zorro-antd/message';
import { timeout } from 'rxjs/operators';
import axios from 'axios';

@Injectable({
  providedIn: 'root'
})
export class CommonService {

  //信息抽取API地址
  public domain: string = 'https://www.fastmock.site/mock/109a1ab7e7d0f6c000c8eb6db9fda9d8/role';

  //FC模块接口地址
  //public fcApiAdd: string = "https://www.fastmock.site/mock/109a1ab7e7d0f6c000c8eb6db9fda9d8/role";
  public fcApiAdd: string = "http://lochost:4200";


  constructor(public http: HttpClient, private message: NzMessageService) { }

  // get(api) {
  //   return new Promise((resolve, reject) => {
  //     this.http.get('assets/url-config.json').subscribe((data: any) => {
  //       this.domain = data.windows.config.url;
  //       this.http.get(this.domain + api).subscribe((response) => {
  //         resolve(response);
  //       })
  //     })
  //   })
  // }

  // post(api, data) {
  //   const httpOptions = {
  //     headers: new HttpHeaders({
  //       'Content-Type': 'application/json'
  //     })
  //   }
  //   return new Promise((resolve, reject) => {
  //     this.http.get('assets/url-config.json').subscribe((data: any) => {
  //       this.domain = data.windows.config.url;
  //       this.http.post(this.domain + api, data, httpOptions).subscribe((response) => {
  //         resolve(response);
  //       })
  //     })
  //   });
  // }

  get(api) {
    return new Promise((resolve, reject) => {
      this.http.get(this.domain + api).pipe(timeout(50000)).subscribe((response: any) => {
        if (response) {
          if (response.error_code == 'AIS.0000') { resolve(response); }
          else {
            if (api.indexOf("query-job") < 0) { this.message.error(response.error_msg); }

          }
        } else {
          reject();
        }
      })
    });
  }

  post(api, data) {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    }
    return new Promise((resolve, reject) => {
      this.http.post(this.domain + api, data, httpOptions).pipe(timeout(50000)).subscribe((response: any) => {
        if (response) {
          if (response.error_code == 'AIS.0000') { resolve(response); }
          else {
            this.message.error(response.error_msg);
          }
        } else {
          reject();
        }
      })
    });
  }

  fcGet(api) {
    return new Promise((resolve, reject) => {
      this.http.get(api).subscribe((response: any) => {
        if (response) {
          if (response) {
            resolve(response);
          }
        } else {
          reject();
        }
      })
    });
  }


  fcPost(api, data) {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Accept': 'application/json;version=8.0;charset=UTF-8',
        'Cookie': 'JSESSIONID=161D3F51638FF2A8114FC84F33FD0DB2'

      })
    }
    return new Promise((resolve, reject) => {
      this.http.post(api, data, httpOptions).subscribe((response: any) => {
        if (response) {
          resolve(response);
        } else {
          reject();
        }
      })
    });
  }

  getPDFdata() {
    return new Promise((resolve, reject) => {
      this.http.get('assets/demo.json').subscribe((data: any) => {
        resolve(data);
      });
    })
  }




  /*
    axiosGet(api) {

      return new Promise((resolve, reject)=>{
        axios.get(api).then((res)=>{
          resolve(res)
        })
      })
    }*/

  axiosGet(api, config) {
    return new Promise((resolve, reject) => {
      axios.get(api, config).then((res) => {
        resolve(res)
      })
    })
  }

  axiosPost(api, data) {
    let config = {
      headers: {
        'Accept': 'application/json;version=8.0;charset=UTF-8',
        'Content-Type': 'application/json'
      }
    }
    return new Promise((resolve, reject) => {
      axios.post(api, data, config).then((res) => {
        resolve(res)
      })

    })

  }
  axiosPostWithConfig(api, data, config) {
    return new Promise((resolve, reject) => {
      axios.post(api, data, config).then((res) => {
        resolve(res)
      })

    })

  }

  axiosPutWithConfig(api, data, config) {
    return new Promise((resolve, reject) => {
      axios.put(api, data, config).then((res) => {
        resolve(res)
      })

    })

  }






}
